#include "../sleipner_config.h"
#include "ogl_graphics.h"

#include "ogl_font.h"
#include "ogl_image_cache.h"
#include "ogl_buffer.h"

#include "ogl_surface.h"
#include "ogl_dynamic_image.h"

#include "util/mmgr.h"	
#include "util/common_macros.h"		// macros
#include "application/game_application.h"
#include "util/timer.h"				// log
#include "util/log.h"				// log


extern "C"
{
#ifdef _SDL2
    
    #include "SDL2/include/SDL.h"
    #include "SDL2/src/video/SDL_sysvideo.h"
    #ifdef _PLATFORM_WIN32
        #include "glee/glee.h"
        //#include "SDL-1.2.13/include/SDL_opengl.h"
    #else
#ifdef _OPGLES2
        #include "SDL2/include/SDL_opengles2.h"
#else
        #include "SDL2/include/SDL_opengles.h"
#endif
    #endif
   
#else
    
    
    #include "SDL-1.2.13/include/SDL.h"
    #ifndef _PLATFORM_IOS
        #include "glee/glee.h"
        //#include "SDL-1.2.13/include/SDL_opengl.h"
    #else
        #include "SDL-1.2.13/include/SDL_opengles.h"
    #endif

#endif
    
}

#include <cassert>

#ifdef WIN32
#include "gameswf/gameswf.h"
gameswf::render_handler*  create_render_handler_ogl(void);
#endif

extern GameApp*	g_pApp;





#if defined(_SDL2) && !defined (_OPENGLES2)

SDL_Window *g_Window = NULL;
SDL_GLContext g_Context = NULL;

const int INDEX_VERTEX = 0;
const int INDEX_TEXTURE = 1;
const int INDEX_COLOR = 2;


bool	OglGraphics::InitWindow(unsigned int Width, unsigned int Height, bool Fullscreen, bool EnableDebug)
{
        
    m_IsWindowed					 = !Fullscreen;
	m_IsWindowed					 = true;
	
	Uint32 Flag = SDL_WINDOW_OPENGL;    
    
    
#ifndef  _PLATFORM_IOS    
	if(!m_IsWindowed)
#endif
    {
		Flag = Flag |SDL_WINDOW_FULLSCREEN;
        
#ifdef  _PLATFORM_IOS 
		Flag = Flag | SDL_WINDOW_BORDERLESS;        
        
#endif
        
    }
    
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); 
    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
    
#if  defined(_PLATFORM_IOS) || defined(_PLATFORM_ANDROID)     
    g_Window = SDL_CreateWindow( NULL, 0, 0, 0, 0, Flag);    
#else    
    g_Window = SDL_CreateWindow( NULL, 0, 0, Width, Height, Flag);    
#endif
    
	g_Context  = SDL_GL_CreateContext(g_Window);
    
  
    Width = g_Window->w;
    Height = g_Window->h;    
    
    m_ScreenWidth  = Width;
	m_ScreenHeight = Height;       
        
    math::Viewport Vp;
	Vp.Width = Width;
	Vp.Height = Height;
	SetViewport(Vp);
    
    
	_LOG(MSG_COMMENT ,"VENDOR: "     << (char*)glGetString( GL_VENDOR ));
	_LOG(MSG_COMMENT ,"RENDERER: "   << (char*)glGetString( GL_RENDERER ));
	_LOG(MSG_COMMENT ,"VERSION: "    << (char*)glGetString( GL_VERSION ));
    _LOG(MSG_COMMENT ,"Width: "      <<  Width );
    _LOG(MSG_COMMENT ,"Height: "     <<  Height);
    
    GraphicsUtil::SetWindowSize( GetGraphics()->GetScreenWidth() , GetGraphics()->GetScreenHeight());
	g_pApp->GetHandler()->SetWidth(GetGraphics()->GetScreenWidth());
	g_pApp->GetHandler()->SetHeight(GetGraphics()->GetScreenHeight());
    
       
	return true;
}

void	OglGraphics::EndScene()
{
	SDL_GL_SwapWindow(g_Window);	
	m_FrameID++;
}

bool	OglGraphics::Shutdown()
{
	SDL_GL_DeleteContext(g_Context);  
  
  // Done! Close the window, clean-up and exit the program. 
	SDL_DestroyWindow(g_Window);

	return true;
}




#endif   // _OPENGLES2


